/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.model.healthmanager.archive.Teeth;
import com.mvc.model.healthmanager.archive.Temperature;
import com.mvc.service.healthmanager.TemperatureServiceI;

/**
 * @author qingjie
 * 体温信息控制器
 */
@RequestMapping("/temperature")
@Controller
public class TemperatureController {
	
	@Autowired
	private TemperatureServiceI tsi;
	
	@RequestMapping("/saveTemperature")
	@ResponseBody
	public Json saveTemperature(HttpServletRequest request,Temperature temperature) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		Temperature t = new Temperature();
		if (!json.isEmpty()) {
			t = JSON.parseObject(json, Temperature.class);
			t.setId(null);
		}
		temperature = tsi.saveOrUpdate(t);
		Json j = new Json();
		if(temperature!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(temperature);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
//	@RequestMapping(value="/saveTemperature", method=RequestMethod.POST,consumes = "application/json")
//	@ResponseBody
//	public Json saveTemperature(@RequestBody Temperature t,HttpServletRequest request,Temperature temperature) throws Exception{
//		t.setId(null);
//		temperature = tsi.saveOrUpdate(t);
//		Json j = new Json();
//		if(temperature!=null){
//			j.setMsg("保存成功");
//			j.setSuccess(true);
//			j.setObj(temperature);
//		}else{
//			j.setMsg("保存失败");
//			j.setSuccess(false);
//		}
//		return j;
//	}
	
	@RequestMapping(value="/saveTemperature", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveTemperature(@RequestBody List<Temperature> ts,HttpServletRequest request) throws Exception{
		System.out.println("未转化的数据："+JSON.toJSONString(ts));
		List<Temperature> list = new ArrayList<Temperature>();
		Json j = new Json();
		if(ts.size()>0){
			for(Temperature t : ts){
				Long appLocalId = ValidatorUtil.isNullOnLong(t.getId())?null:t.getId();
				t.setId(null);
				Temperature temperature = new Temperature();
				temperature = tsi.saveOrUpdate(t);
				if(temperature!=null&&!ValidatorUtil.isNullOnLong(temperature.getId())){
					temperature.setRespCode(0);
				}else{
					temperature.setRespCode(-1);
				}
				temperature.setAppLocalId(appLocalId);
				list.add(temperature);
			}
			j.setMsg("数据已处理");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/getNewTemperature")
	@ResponseBody
	public Json getNewTemperature(Temperature t){
		Json j = new Json();
		Temperature temperature = tsi.getNewTemperature(t);
		if(!ValidatorUtil.isNullOnLong(temperature.getId())){
			j.setMsg("获取成功");
			j.setSuccess(true);
			j.setObj(temperature);
		}else{
			j.setMsg("获取数据为空");
			j.setSuccess(false);
		}
		return j;
	}
}
